package sg.edu.nus.iss.vmcs.maintenance;

import java.awt.*;

import sg.edu.nus.iss.vmcs.store.*;
import sg.edu.nus.iss.vmcs.system.*;
import sg.edu.nus.iss.vmcs.machinery.*;
import sg.edu.nus.iss.vmcs.util.*;

public class MaintenanceController {

	private MainController mCtrl;
	private MaintenancePanel mpanel;
	private AccessManager am;

	public MaintenanceController(MainController mctrl) {
		mCtrl = mctrl;
		am = new AccessManager(this);
	}

	public MainController getMainController() {
		return mCtrl;
	}

	/**
	 * setup the maintenance panel and display it.
	 * @throws VMCSException 
	 */
	public void displayMaintenancePanel() throws VMCSException {
		//SimulatorControlPanel scp = mCtrl.getSimulatorControlPanel();
		if (mpanel == null)
			mpanel =  new MaintenancePanel(this);
		mCtrl.registerObservers(Store.CASH, mpanel); // register for observer
		mCtrl.registerObservers(Store.DRINK, mpanel);// register for observer
		
		mpanel.display();
		mpanel.setActive(MaintenancePanel.DIALOG, true);
		// setActive of password, invalid and valid display.
	}

	public AccessManager getAccessManager() {
		return am;
	}

	public void loginMaintainer(boolean st) {
		mpanel.displayPasswordState(st);
		mpanel.clearPassword();
		if (st == true) {
			// login successful
			mpanel.setActive(MaintenancePanel.WORKING, true);
			mpanel.setActive(MaintenancePanel.PSWD, false);
			MachineryController machctrl = mCtrl.getMachineryController();
			machctrl.setDoorState(false);
		}
	}

	// invoked in CoinDisplayListener
	public void displayCoin(int idx) {

		StoreService sctrl = mCtrl.getStoreService();
		CashStoreItem item;
		try {
			item = (CashStoreItem) sctrl.getStoreItem(Store.CASH, idx);
			mpanel.getCoinDisplay().displayQty(idx, item.getQuantity());
		} catch (VMCSException e) {
			System.out.println("MaintenanceController.displayCoin:" + e);
		}

	}

	// invoked in DrinkDisplayListener;
	public void displayDrinks(int idx) {

		StoreService sctrl = mCtrl.getStoreService();
		DrinksStoreItem item;
		try {
			item = (DrinksStoreItem) sctrl.getStoreItem(Store.DRINK, idx);
			DrinksBrand db = (DrinksBrand) item.getContent();
			mpanel.getDrinksDisplay().displayQty(idx, item.getQuantity());
			mpanel.displayPrice(db.getPrice());
		} catch (VMCSException e) {
			System.out.println("MaintenanceController.displayDrink:" + e);
		}

	}

	// invoked by PriceDisplayListener
	public void setPrice(int pr) {

		StoreService sctrl = mCtrl.getStoreService();
		int curIdx = mpanel.getCurIdx();
		sctrl.setPrice(curIdx, pr);
		mpanel.getDrinksDisplay().getPriceDisplay().setValue(pr + "C");
	}

	// TotalCashButtonListener
	public void getTotalCash() {

		StoreService sctrl = mCtrl.getStoreService();
		int tc = sctrl.getTotalCash();
		mpanel.displayTotalCash(tc);

	}

	// TransferCashButtonListener
	public void transferAll() {
		StoreService sctrl = mCtrl.getStoreService();
		int cc; // coin quantity;
		cc = sctrl.transferAll();
		mpanel.displayCoins(cc);

	}

	// StoreViewerListener
	public void changeStoreQty(char type, int idx, int qty) {

		try {
			mpanel.updateQtyDisplay(type, idx, qty);
			mpanel.initCollectCash();
			mpanel.initTotalCash();
		} catch (VMCSException e) {
			System.out.println("MaintenanceController.changeStoreQty:" + e);
		}
	}

	// exit button listener;
	public void logoutMaintainer() {

		MachineryController machctrl = mCtrl.getMachineryController();

		boolean ds = machctrl.isDoorClosed();

		if (ds == false) {
			MessageDialog msg = new MessageDialog(mpanel,
					"Please Lock the Door before You Leave");
			msg.setLocation(500, 500);
			return;
		}

		mpanel.setActive(MaintenancePanel.DIALOG, true);

	}

	public void closeDown() {
		if (mpanel != null)
			mpanel.closeDown();
	}

}